package leetcode;

import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 东莞呵呵
 * Date:2022-07-15
 * Time:17:50
 */
public class MinStack {
    public Stack<Integer> stack;
    public Stack<Integer> min_stack;
    public MinStack() {
        this.stack=new Stack<>();
        this.min_stack=new Stack<>();
    }

    public void push(int val) {
        stack.push(val);
        if(min_stack.empty()){
            min_stack.push(val);
        }else{
            if(val<=min_stack.peek()){
                min_stack.push(val);
            }
        }
    }

    public void pop() {
        if(stack.isEmpty()){
            return;
        }
        //包装类不能直接比大小要先拆包或者使用equals
//        if(stack.peek()==min_stack.peek()){
//            min_stack.pop();
//            stack.pop();
//        }
        if(stack.pop().equals(min_stack.peek())){
            min_stack.pop();
        }
    }

    public int top() {
        if(stack.isEmpty()){
            return -1;
        }
        return stack.peek();
    }

    public int getMin() {
        if(min_stack.isEmpty()){
            return -1;
        }
        return min_stack.peek();
    }



}
